import base64
import cv2
import requests
from PyQt5.QtCore import QTimer
from deleteuser import Ui_Dialog
from PyQt5.QtWidgets import QDialog, QMessageBox


class deluserwindow(Ui_Dialog, QDialog):

    def __init__(self, list, token, parent=None):
        super(deluserwindow, self).__init__(parent)
        self.setupUi(self)  # 创建界面内容

        self.access_token = token

        self.show_grouplist(list)

        self.pushButton_3.clicked.connect(self.get_user)
        self.pushButton_4.clicked.connect(self.get_facetoken)
        self.pushButton.clicked.connect(self.get_ok)
        self.pushButton_2.clicked.connect(self.close_window)

    def show_grouplist(self, list):
        for i in list:
            self.listWidget.addItem(i)

    def get_user(self):
        group = self.listWidget.currentItem().text()
        list = self.getuserlist(group)
        print(list['result']['user_id_list'])
        for i in list['result']['user_id_list']:
            self.listWidget_2.addItem(i)

    def get_facetoken(self):
        group = self.listWidget.currentItem().text()
        user = self.listWidget_2.currentItem().text()
        face_list = self.user_face_list(group, user)
        for i in face_list['result']['face_list']:
            self.listWidget_3.addItem(i['face_token'])

    # 获取用户列表
    def getuserlist(self, group):
        request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getusers"
        params = {
            "group_id": group
        }
        access_token = self.access_token
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/json'}
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            return response.json()

    # 删除用户中的一张人脸信息(face_token)
    def del_face_token(self, group, user, face_token):
        request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/delete"
        params = {
            "user_id": user,
            "group_id": group,
            "face_token": face_token
        }
        access_token = self.access_token
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/json'}
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            data = response.json()
            if data['error_code'] == 0:
                QMessageBox.about(self, "人脸删除结果", "人脸删除成功")
            else:
                QMessageBox.about(self, "人脸删除结果", "人脸删除失败" + data['error_msg'])

    # 获取用户人脸列表
    def user_face_list(self, group, user):
        request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist"

        params = {
            "user_id": user,
            "group_id": group
        }
        access_token = self.access_token
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/json'}
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            return response.json()

    def get_ok(self):
        group = self.listWidget.currentItem().text()
        user = self.listWidget_2.currentItem().text()
        face_token = self.listWidget_3.currentItem().text()
        self.del_face_token(group, user, face_token)

    # 点击取消按钮
    def close_window(self):
        # 关闭对话框
        self.reject()